//
//  RegularBusLineViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/15.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "ThroughBusLineViewController.h"
#import "ThroughBusLineViewModel.h"
#import "CalendarViewController.h"
#import "KDateFormatter.h"
#import "NSDate+WQCalendarLogic.h"
#import "ThroughBusLineTableCell.h"
#import "RoutePlanningViewController.h"
#import "ResponseThroughBusLineEntity.h"

@interface ThroughBusLineViewController ()


@property (weak, nonatomic) IBOutlet UIView *topBgView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLine_layout;

- (IBAction)leftBtnAction:(UIButton *)sender;

- (IBAction)rightBtnAction:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UIButton *centerBtn;

- (IBAction)centerBtnAction:(UIButton *)sender;

@property (strong, nonatomic) CalendarViewController *dayTicket;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomLine_height;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomLine_width;
@property (weak, nonatomic) IBOutlet UIView *bottomBGView;

// 时间排序
- (IBAction)timeSortAction:(UIButton *)sender;
// 价格排序
- (IBAction)priceSortAction:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UITableView *mainTable;


@end



@implementation ThroughBusLineViewController

-(void)awakeFromNib{
    self.throughBusLineViewModel = [[ThroughBusLineViewModel alloc ]init];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLeftBtnItem];
     self.navigationItem.title = @"搜索结果";
    self.centerBtn.layer.cornerRadius = 3;
    self.centerBtn.layer.masksToBounds= YES;
    self.topLine_layout.constant = 0.45;
    self.bottomLine_height.constant = 0.45;
    self.bottomLine_width.constant = 0.45;
    
    
    if (self.selectedData != nil) {
        [self.centerBtn setTitle:self.selectedData forState:UIControlStateNormal];
    }else{
         [self.centerBtn setTitle:[KDateFormatter dateToStringDate:[NSDate date] formatString:KDateFormatterStyle4] forState:UIControlStateNormal];
    }
  
    
    [self setExtraCellLineHidden:self.mainTable];
    
    self.throughBusLineViewModel.requestThroughBusLineEntity.Order = 1;
   
    
    [self loadData];
    
    self.mainTable.mj_footer = [MJDIYBackFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    
    // 头部刷新
    WEAKSELF
    [self addHeader:^() {
        [weakSelf loadNewData];
    } tableView:self.mainTable];
}

-(void)backAction{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)loadData{
    
    self.throughBusLineViewModel.requestThroughBusLineEntity.SendData = self.centerBtn.titleLabel.text;
    
    WEAKSELF
    [self.throughBusLineViewModel loadThroughBusLineList:^(NSInteger resultType, id data, NSString *errorMsg) {
        [self.mainTable.mj_header endRefreshing];
        [self.mainTable.mj_footer endRefreshing];
        
        if (resultType == 1 || resultType == 3) {
            [weakSelf.mainTable reloadData];
            
            if (resultType == 3) {
                weakSelf.mainTable.mj_footer.hidden = YES;
                
            }
            else{
                weakSelf.mainTable.mj_footer.hidden = NO;
            }
        }else{
            [SVProgressHUD showErrorWithStatus:errorMsg];
        }
        
      
    }];
}

-(void)loadNewData{
    self.throughBusLineViewModel.pageIndex = 1;
    [self loadData];
}

-(void)loadMoreData{
    self.throughBusLineViewModel.pageIndex ++;
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// 上一天
- (IBAction)leftBtnAction:(UIButton *)sender {
    
    NSDate *lastDay = [KDateFormatter stringToDateString:self.centerBtn.titleLabel.text formatString:KDateFormatterStyle4];
    if ([NSDate isCurrentDay:lastDay]) {
        return;
    }
    
    NSString *lastText = [KDateFormatter dateToStringDate:[NSDate lastDay:lastDay] formatString:KDateFormatterStyle4];
    self.centerBtn.titleLabel.text = lastText;
    [self.centerBtn setTitle:lastText forState:UIControlStateNormal];
    self.throughBusLineViewModel.requestThroughBusLineEntity.SendData = lastText;
    [self loadNewData];
}

// 下一天
- (IBAction)rightBtnAction:(UIButton *)sender {
    NSDate *nextDay = [KDateFormatter stringToDateString:self.centerBtn.titleLabel.text formatString:KDateFormatterStyle4];
    NSString *currentShowDay = [KDateFormatter dateToStringDate:nextDay formatString:KDateFormatterStyle4];
    if ([currentShowDay isEqualToString:self.throughBusLineViewModel.showMaxDateString]) {
        return;
    }
    
    NSString *nextText = [KDateFormatter dateToStringDate:[NSDate nextDay:nextDay] formatString:KDateFormatterStyle4];
    self.centerBtn.titleLabel.text = nextText;
    [self.centerBtn setTitle:nextText forState:UIControlStateNormal];
    self.throughBusLineViewModel.requestThroughBusLineEntity.SendData = nextText;
    [self loadNewData];
}

- (IBAction)centerBtnAction:(UIButton *)sender {
    
    self.dayTicket = [[CalendarViewController alloc ] init];
    [self.dayTicket responseDayModels:[self.throughBusLineViewModel getThroughBusLineDateList]];
    [self.dayTicket ajustFrame:CGRectMake(0, 0,  [UIScreen width], [UIScreen height] -64)];
    [self.dayTicket setSelectedDate:@[self.centerBtn.titleLabel.text]];
    WEAKSELF
    self.dayTicket.calendarblock = ^void(CalendarDayModel *model){
        [weakSelf.centerBtn setTitle:model.toString forState:UIControlStateNormal];
        [weakSelf loadNewData];
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    [self.navigationController pushViewController:self.dayTicket animated:YES];
}

- (IBAction)timeSortAction:(UIButton *)sender {
     self.throughBusLineViewModel.requestThroughBusLineEntity.Order = 1;
     [self loadNewData];
}

- (IBAction)priceSortAction:(UIButton *)sender {
     self.throughBusLineViewModel.requestThroughBusLineEntity.Order = 2;
     [self loadNewData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    ResponseThroughBusLineEntity *model = self.throughBusLineViewModel.throughBusLineList[indexPath.row];
    
    
    RoutePlanningViewController *routePlanningViewController = (RoutePlanningViewController *)[RoutePlanningViewController getViewControllerFromSbName:@"RoutePlanning" viewControllerName:@"RoutePlanningViewController"];
    routePlanningViewController.routePlanningStatus = RoutePlanningThroughBus;
    routePlanningViewController.lineId = @(model.LineId).stringValue;
    routePlanningViewController.StartStationId = model.StartStationId;
    routePlanningViewController.EndStationId  = model.EndStationId;
    routePlanningViewController.BCID  = model.BCID;
    routePlanningViewController.TicketTime = self.centerBtn.titleLabel.text;
    [self.navigationController pushViewController:routePlanningViewController animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
  
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.throughBusLineViewModel.throughBusLineList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    

    ThroughBusLineTableCell *lineCell = [tableView dequeueReusableCellWithIdentifier:[ThroughBusLineTableCell cellIdentifier]];
    [lineCell showModel:self.throughBusLineViewModel.throughBusLineList[indexPath.row]];
    return lineCell;
  
}

@end
